/**
 *查询设备
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");

module.exports = async function (params) {
    let querySql =`SELECT
                        df.device_id AS deviceId,
                        df.device_no AS deviceNo,
                        df.device_type AS deviceType,
                        df.device_name AS deviceName,
                        rf.range_name AS rangeName,
                        df.range_id AS rangeId,
                        df.way_type AS wayType,
                        df.cost_plan_id AS costPlanId,
                        CONVERT(df.enable,CHAR) AS enable,
                        df.road,
                        df.parent_id AS parentId,
                        cdf.device_id AS cDeviceId,
                        cdf.device_no AS cDeviceNo,
                        cdf.device_type AS cDeviceType,
                        cdf.device_name AS cDeviceName,
                        cdf.parent_id AS cParentId,
                        cdf.way_type AS cWayType,
                        cdf.cost_plan_id AS cCostPlanId,
                        CONVERT(cdf.enable,CHAR) AS cEnable,
                        cdf.road AS cRoad,
                        crf.range_id AS cRangeId,
                        crf.range_name AS cRangeName
                    FROM
                        device_file df 
                        INNER JOIN range_file rf ON rf.range_id = df.range_id AND rf.delete_sign = 1
                        LEFT JOIN device_file cdf ON cdf.parent_id = df.device_id AND cdf.delete_sign = 1 
                        LEFT JOIN range_file crf ON crf.range_id = cdf.range_id AND crf.delete_sign = 1 
                    WHERE
                    df.parent_id = 0 AND 
                    df.delete_sign = 1 AND df.range_id IN (?) 
                    ${params.deviceType ? `AND df.device_type = ${params.deviceType}`:``}
                    ${params.searchText ? `AND (df.device_name LIKE '%${params.searchText}%' OR df.device_no LIKE '%${params.searchText}%'` : ""}`
    let deviceList =  await conn(querySql,[params.ids]);
    let cParentGroup = _.groupBy(deviceList,"cParentId");
    let dataList =[];
    for(const cParentId in cParentGroup){
        let cParentList = cParentGroup[cParentId];
        if(cParentId === "null"){
          for(const parentDevice of cParentList){
              dataList.push({
                  deviceId:parentDevice.deviceId,
                  deviceNo:parentDevice.deviceNo,
                  deviceType:parentDevice.deviceType,
                  deviceName:parentDevice.deviceName,
                  rangeName:parentDevice.rangeName,
                  rangeId:parentDevice.rangeId,
                  costPlanId:parentDevice.costPlanId,
                  wayType:parentDevice.wayType,
                  enable:parentDevice.enable,
                  road:parentDevice.road,
                  parentId:parentDevice.parentId,
              })
          }
        }else{
          let parentObj = {
              deviceId:cParentList[0].deviceId,
              deviceNo:cParentList[0].deviceNo,
              deviceType:cParentList[0].deviceType,
              deviceName:cParentList[0].deviceName,
              rangeName:cParentList[0].rangeName,
              rangeId:cParentList[0].rangeId,
              costPlanId:cParentList[0].costPlanId,
              wayType:cParentList[0].wayType,
              enable:cParentList[0].enable,
              road:cParentList[0].road,
              parentId:cParentList[0].parentId,
              children:[],
          }
          for(const childrenDevice of cParentList){
              parentObj.children.push({
                  deviceId:childrenDevice.cDeviceId,
                  deviceNo:childrenDevice.cDeviceNo,
                  deviceType:childrenDevice.cDeviceType,
                  deviceName:childrenDevice.cDeviceName,
                  rangeName:childrenDevice.cRangeName,
                  rangeId:childrenDevice.cRangeId,
                  costPlanId:childrenDevice.cCostPlanId,
                  wayType:childrenDevice.cWayType,
                  enable:childrenDevice.cEnable,
                  road:childrenDevice.cRoad,
                  parentId:childrenDevice.cParentId,
              })
          }
            dataList.push(parentObj);
        }
    }

    return { success: true ,deviceList:dataList};
};